package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alan.common.constants.ELogType;
import net.alan.common.utils.MD5Util;
import net.alan.common.utils.MapUtils;
import net.alan.modules.aspect.AdminRole;
import net.alan.modules.aspect.NotAuthorize;
import net.alan.modules.bean.Result;
import net.alan.modules.bean.ResultUtil;
import net.alan.modules.bean.User;
import net.alan.modules.bean.WorkStat;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.service.INoticeService;
import net.alan.modules.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */

@Api(tags = "后台管理")
@Controller
@RequestMapping(value = "home")
public class HomeController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    @Autowired
    private IUserService userService;
    @Autowired
    private INoticeService noticeService;

    @NotAuthorize
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String index() {
        logger.info("登陆首页");
        return "web/login";
    }

    @NotAuthorize
    @RequestMapping(value = "findpass", method = RequestMethod.GET)
    public String findpass(String code, String sessionId, Model model) {
        return "web/findpass1";
    }

    @NotAuthorize
    @RequestMapping(value = "my/findpass", method = RequestMethod.GET)
    public String getfindpass(String code, String sessionId, Model model) {
        model.addAttribute("code", code);
        model.addAttribute("sessionId", sessionId);
        return "web/findpass2";
    }

    @NotAuthorize
    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String register() {
        return "web/register";
    }

    @NotAuthorize
    @ApiOperation(value = "登陆")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(
            String account, String password, HttpServletRequest request) throws Exception {
        Result result = null;
        User user = userService.getUser(account, true);
        if (user == null) {
            result = ResultUtil.error("用户不存在");
        } else if (!user.getPassword().equals(MD5Util.md5(password))) {
            result = ResultUtil.error("密码不匹配");
        } else {
            WebContextUtil.login(user);
            addLog(ELogType.login, request);
            result = ResultUtil.success();
        }
        return result;
    }

    @ApiOperation(value = "注销")
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(HttpSession session) throws Exception {
        WebContextUtil.onlineMap.remove(session.getId());
        WebContextUtil.logout();
        return "web/login";
    }

    @RequestMapping(value = "main", method = RequestMethod.GET)
    public String main(Model model) throws Exception {
        if (WebContextUtil.isAdmin()) {
            List<WorkStat> list = userService.findWorkStat();
            if (list != null) {
                for (WorkStat ws : list) {
                    model.addAttribute(ws.getName(), ws.getValue());
                }
            }
            return "web/main_admin";
        }
        model.addAttribute("notice", noticeService.countUnRead(WebContextUtil.getUserId()));
        model.addAllAttributes(MapUtils.ObjToMap(WebContextUtil.getCurrentUser()));
        return "web/main_user";
    }

    /**
     * 云笔记
     */
    @RequestMapping(value = "cloudnote", method = RequestMethod.GET)
    public String cloudNote(Model model) throws Exception {
        return "web/cloud/cloudnote";
    }

    /**
     * 云收藏
     */
    @RequestMapping(value = "cloudfavorite", method = RequestMethod.GET)
    public String cloudFavorite(Model model) throws Exception {
        return "web/cloud/cloudfavorite";
    }

    /**
     * 云协作
     */
    @RequestMapping(value = "cloudwork", method = RequestMethod.GET)
    public String cloudWork(Model model) throws Exception {
        return "web/cloud/cloudwork";
    }

    /**
     * 用户页面
     */
    @AdminRole
    @RequestMapping(value = "userall", method = RequestMethod.GET)
    public String userall(Model model) throws Exception {
        return "web/admin/userall";
    }

    /**
     * 项目页面
     */
    @AdminRole
    @RequestMapping(value = "projectall", method = RequestMethod.GET)
    public String projectall(Model model) throws Exception {
        return "web/admin/projectall";
    }

    /**
     * 系统参数
     */
    @AdminRole
    @RequestMapping(value = "setting", method = RequestMethod.GET)
    public String setting(Model model) throws Exception {
        return "web/admin/setting";
    }

    /**
     * 系统通知
     */
    @AdminRole
    @RequestMapping(value = "broadcast", method = RequestMethod.GET)
    public String broadcast(Model model) throws Exception {
        return "web/admin/broadcast";
    }

    /**
     * 用户反馈管理
     */
    @AdminRole
    @RequestMapping(value = "feedback", method = RequestMethod.GET)
    public String feedback(Model model) throws Exception {
        return "web/admin/feedback";
    }

}
